package com.ruoyi.web.controller.business;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.ExcelExporter;
import com.ruoyi.common.utils.file.ExcelImporter;
import com.ruoyi.system.domain.CaiGouDan;
import com.ruoyi.system.domain.CangKu;
import com.ruoyi.system.domain.HuoPin;
import com.ruoyi.system.domain.RuKuDan;
import com.ruoyi.system.domain.vo.CaiGouDanQueryReq;
import com.ruoyi.system.domain.vo.CreateRuKuDanByCaiGouDanRequest;
import com.ruoyi.system.domain.vo.Ids;
import com.ruoyi.system.service.ICaiGouDanService;
import com.ruoyi.system.service.IHuoPinService;
import com.ruoyi.system.service.IRuKuDanService;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 入库单信息 前端控制器
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-20
 */
@Log4j2
@RestController
@RequestMapping("/system/rukudan")
public class RuKuDanController extends BaseController {

    @Resource
    private IRuKuDanService service;

    @Resource
    private IHuoPinService huoPinService;

    /**
     * 获取列表
     */
    @GetMapping("/listWithPage")
    public TableDataInfo listWithPage(RuKuDan req)
    {
        startPage();
        List<RuKuDan> list = service.selectRuKuDanList(req);
        return getDataTable(list);
    }

    /**
     * 导出列表，不分页
     */
    @PostMapping("/export")
    public void export(RuKuDan req, HttpServletResponse rsp) throws IOException {
        List<RuKuDan> list = service.selectRuKuDanList(req);

        List<List<String>> datas = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        titles.add("货品");
        titles.add("入库数量");
        titles.add("单位");
        titles.add("品牌");
        titles.add("入库日期");
        titles.add("供应商");
        titles.add("生产日期");
        titles.add("保质期");
        titles.add("生产商");
        datas.add(titles);
        for (RuKuDan ruKuDan : list) {
            List<String> data = new ArrayList<>();
            data.add(ruKuDan.getHuoPinName());
            data.add(ruKuDan.getRuKuCount().toPlainString());
            data.add(ruKuDan.getDanWei());
            data.add(ruKuDan.getPinPai());
            data.add(ruKuDan.getRuKuTime().toString());
            data.add(ruKuDan.getGongYingShangName());
            data.add(ruKuDan.getShengChanRiQi());
            data.add(ruKuDan.getBaoZhiQi());
            data.add(ruKuDan.getShengChanShang());

            datas.add(data);
        }

        ExcelExporter.exportToExcel(datas, rsp.getOutputStream());
    }


    @PostMapping("/importExcel")
    public AjaxResult importExcel(MultipartFile file) throws IOException {
        try {
            List<List<String>> list = ExcelImporter.importExcel(file);
            log.info(list);

            service.saveImportExcel(list);

            return toAjax(true);
        } catch (Exception e) {
            log.warn(e.getMessage(), e);
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 根据采购单生成对应的入库单
     * @param infos 采购单信息
     * @return 返回 .
     */
    @PostMapping("/createByCaiGouDan")
    public AjaxResult createByCaiGouDan(@RequestBody CreateRuKuDanByCaiGouDanRequest infos){
        boolean ok = service.createByCaiGouDan(infos.getList(), getUsername());
        return toAjax(ok);
    }

    /**
     * 获取详情
     */
    @GetMapping("/{id}")
    public AjaxResult getDetail(@PathVariable(value = "id") Long id)
    {
        RuKuDan ruKuDan = service.getById(id);
        HuoPin huoPin = huoPinService.getById(ruKuDan.getHuoPinId());
        if(huoPin != null){
            ruKuDan.setHuoPinName(huoPin.getName());
        }
        return AjaxResult.success(ruKuDan);
    }


    @GetMapping("/queryAuditInfo/{id}")
    public AjaxResult queryAuditInfo(@PathVariable(value = "id") Long id){
        RuKuDan ruKuDan = service.queryAuditInfo(id);
        return AjaxResult.success(ruKuDan);
    }

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RuKuDan user)
    {
        user.setBatchId(UUID.randomUUID().toString());
        user.setCreateBy(getUsername());
        user.setCreateTime(new Date());
        user.setStatus(0);
        return toAjax(service.save(user));
    }


    /**
     * 修改
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RuKuDan user)
    {
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        return toAjax(service.updateById(user));
    }

    /**
     * 修改
     */
    @PutMapping("/audit")
    public AjaxResult audit(@Validated @RequestBody RuKuDan user)
    {
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());

        boolean ok = service.audit(user);
        if(ok){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

}
